package com.lmh.controller;

import com.lmh.pojo.ProjectType;
import com.lmh.pojo.ProjectTypeVo;
import com.lmh.service.ProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class ProjectTypeController {

    @Autowired
    private ProjectTypeService projectTypeService;

    @RequestMapping("/demandtype")
    public String demandtype(Model m){
        List<ProjectType> projectTypes=projectTypeService.projecttypeall();
        if(projectTypes !=null){
            m.addAttribute("projectTypes",projectTypes);
            return "demandtype";
        }else {
            return "";
        }

    }

    @RequestMapping("/projecttypelike")
    @ResponseBody
    public List<ProjectType> projecttypelike(@RequestBody ProjectTypeVo projectTypeVo){
        List<ProjectType> projectTypes=projectTypeService.projecttypelike(projectTypeVo.getProhectTypeName());
        System.out.println(projectTypes);
        return projectTypes;
    }

    @RequestMapping("/projecttypedelete")
    public String projecttypedelete(String projectTypeId){
        int i=projectTypeService.projecttypedelete(projectTypeId);
        if(i !=0){
            return "redirect:/demandtype";
        }else {
            return "";
        }
    }

    @RequestMapping("/projecttypeadd")

    public List<ProjectType> projecttypeadd(@RequestBody ProjectTypeVo projectTypeVo){
        ProjectType projectType=new ProjectType();
        projectType.setProhectTypeName(projectTypeVo.getProhectTypeName());
        int count=projectTypeService.count();
        projectType.setProjectTypeId(count+1);
        projectType.setCreateTime(new Date());
        projectType.setUpdateTime(new Date());
        int i=projectTypeService.projecttypeadd(projectType);
        String s="%"+" "+"%";
        List<ProjectType> projectTypes=projectTypeService.projecttypelike(s);
        return projectTypes;


    }

    @RequestMapping("/text")
    public String text(){
        return "text";
    }

}
